import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_sign_record_entity.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../duty_dialog/duty_dialog_logic.dart';
import 'duty_cancel_dialog_state.dart';

class DutyCancelDialogLogic extends DutyDialogLogic {
  final DutyCancelDialogState dialogState = DutyCancelDialogState();

  late TextEditingController cancelReason;

  @override
  void onInit() {
    super.onInit();
    cancelReason = TextEditingController();
  }

  @override
  void onReady() {
    super.onReady();
    postListSignRecord(state.collectId);
  }

  /// 获取值班日期报名列表
  Future<void> postListSignRecord(String id) async {
    Map<String, dynamic> param = {'id': id};
    ResultData resultData = await Api.postListSignRecord(param);
    if (resultData.isSuccess) {
      List<dynamic> list = jsonDecode(jsonEncode(resultData.data['data']));
      dialogState.listSignRecordEntity = list.map((e) => ListSignRecordEntity.fromJson(e)).toList();
      update();
    } else {
      toast.show(resultData.data["message"]);
    }
  }

  /// 窗口值班 取消报名
  Future<void> postCancelScheduleSign(String workDate) async {
    if (cancelReason.text == "") {
      return toast.show("请输入取消值班的原因");
    }

    Map<String, dynamic> param = {
      "cancelReason": cancelReason.text, // 取消原因
      "collectId": state.collectId, // 征集表ID
      "workDate": workDate // 	值班日期
    };
    ResultData resultData = await Api.postCancelScheduleSign(param);
    if (resultData.isSuccess) {
      Get.back();
      postListSignRecord(state.collectId);
      customCalendarKey.currentState?.upDateSelectedDays(workDate);
      update();
    } else {
      toast.show(resultData.data["message"]);
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
